import {
  Controller,
  Get,
  Body,
  Put,
  Param,
  Delete,
  Query,
} from '@nestjs/common';
import { UserService } from './user.service';
import { UpdateUserDto } from './dto/update-user.dto';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiQuery,
  ApiTags,
} from '@nestjs/swagger';
import { Public } from 'src/common/decorators/public.decorator';

@Controller('user')
@ApiTags('用户管理')
@ApiBearerAuth()
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Get()
  @ApiOperation({
    description: '查询所有用户',
    summary: '查询所有用户',
  })
  findAll() {
    return this.userService.findAll();
  }

  @Get('/findall')
  @ApiOperation({
    description: '根据管理员的所在区查找用户',
  })
  @ApiQuery({
    name: 'address',
    description: '管理员所在地区的用户',
    required: true,
  })
  @ApiQuery({
    name: 'keywords',
    description: '通过关键词查询',
    required: false,
  })
  findUserByAddress(@Query('address') address, @Query('keywords') keywords) {
    return this.userService.findUserByAddress(address, keywords);
  }

  @Get('/skip')
  @ApiOperation({
    summary: '分页查询',
    description: '分页查管理员所在地区的用户信息',
  })
  @ApiQuery({ name: 'pageSize', description: '数据条数', required: true })
  @ApiQuery({ name: 'currentPage', description: '当前页码', required: true })
  @ApiQuery({ name: 'role', description: '角色', required: true })
  @ApiQuery({ name: 'city', description: '城市', required: true })
  @ApiQuery({ name: 'keywords', description: '关键词', required: false })
  skipFind(
    @Query('currentPage') currentPage,
    @Query('pageSize') pageSize,
    @Query('role') role,
    @Query('city') city,
    @Query('keywords') keywords,
  ) {
    return this.userService.skipFind(
      +currentPage,
      +pageSize,
      role,
      city,
      keywords,
    );
  }

  @Get(':id')
  @ApiOperation({
    description: '根据用户的id查询当前用户',
    summary: '根据id查询用户',
  })
  findOne(@Param('id') id: string) {
    return this.userService.findOne(+id);
  }

  @Get('/get_order')
  @Public()
  async findUserOrders(@Query('id') id: string) {
    return await this.userService.findUserOrder(+id);
  }

  @Put()
  @ApiOperation({
    description:
      '更新用户信息，该传入的数据中一定要包含当前修改的id,否则会出现错误',
    summary: '更新用户信息',
  })
  update(@Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(updateUserDto);
  }

  @Put('/update_password/:id')
  updatePassword(@Param('id') id, @Body() params) {
    return this.userService.updatePassword(id, params.password);
  }

  @Delete('/delete')
  @ApiOperation({
    description: '根据id一个用户',
    summary: '根据id删除用户',
  })
  remove(@Body() ids: number[]) {
    return this.userService.remove(ids);
  }
}
